package promote.brozen.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author Brozen
 * @date 2020/9/30 3:29 PM
 * @email brozen@qq.com
 */
public class BufReadWrite {


    public static void main(String[] args) {

        ByteBuf buf = Unpooled.buffer();
        printIndex(buf);

        // 顺序写
        buf.writeBytes(new byte[]{1, 2, 3, 4, 5 });
        printIndex(buf);

        // 顺序读
        System.out.println("读取：" + buf.readByte());
        printIndex(buf);

        // 随机写
        System.out.println("随机写入索引3 = 99");
        buf.setByte(3, 99);
        printIndex(buf);

        // 随机读
        System.out.println("读取索引3：" + buf.getByte(3));
        printIndex(buf);
    }

    private static void printIndex(ByteBuf buf) {
        System.out.println(String.format("readerIndex: %s wirterIndex: %s", buf.readerIndex(), buf.writerIndex()));
    }


}
